/*
 * Copyright 2003-2005 (c) Live Media Pty Ltd. <argot@einet.com.au> 
 *
 * This software is licensed under the Argot Public License 
 * which may be found in the file LICENSE distributed 
 * with this software.
 *
 * More information about this license can be found at
 * http://www.einet.com.au/License
 * 
 * The Developer of this software is Live Media Pty Ltd,
 * PO Box 4591, Melbourne 3001, Australia.  The license is subject 
 * to the law of Victoria, Australia, and subject to exclusive 
 * jurisdiction of the Victorian courts.
 */

#include "../Types.h"
#include "string.h"
#include "U8Ascii.h"

type_name u8ascii_name = { "u8ascii" };

int u8ascii_read(type_input_stream * in, type_element * element, void * data )
{
	/* The below line won't work because we don't know what U8BID
	//is registered as in the remote system.
	//Byte id = (Byte) in.readObject( TypeMapCore.U8BID );
	//We can either use the element to find the reference object
	//in the sequence definition or cheat and just read a byte.
	//int id = in.getStream()->get();
	 */
	int i;
	unsigned char id;

	i = type_input_read_buffer( in, (char *) &id, 1 );	
	if ( i != 1 ) 
	{
		type_input_set_error( in, TYPE_EOF );
		return TYPE_EOF;
	}	 
	
	if ( id > 0 )
	{
		char * str = type_malloc( id+1 );
		
		type_input_read_buffer( in, str, id );
		str[id]=0;
		
		type_stack_set_ptr( &in->stack, element, str );
	}
	else
	{
		char * str = type_malloc( 1 );
		str[0] = 0;
		type_stack_set_ptr( &in->stack, element, str );
	}
	return TYPE_OK;
}


int u8ascii_write(type_output_stream * out, void * o, type_element * element, void * data )
{
	char * str = (char *) o;
	int size = 0;
		
	if ( o == NULL )
	{
		type_output_write_buffer( out, (char *) &size, 1 );
		return TYPE_OK;
	}

	size = strlen( str );

	type_output_write_buffer( out, (char *) &size, 1 );
	type_output_write_buffer( out, str, size );
	return TYPE_OK;
}
